package com.sright.bcard.enterprise.web.task;

import com.sright.bcard.common.service.share.utils.JedisUtil;
import com.sright.bcard.user.biz.manager.AdminManager;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

@Slf4j
public class DayFirstLoginJob extends QuartzJobBean {

    @Autowired
    private AdminManager adminManager;

    @Autowired
    private JedisUtil jedisUtil;

    private final static String UPDATE_DAY_FIRST_LOGIN = "UPDATE_DAY_FIRST_LOGIN";

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) {
        Boolean lock = jedisUtil.getLock(UPDATE_DAY_FIRST_LOGIN, 300000L);
        if(lock) {
            try {
                //获取JobDetail中关联的数据
                Integer result = adminManager.updateDayFirstLogin();
                log.info("每日首次登录标识修改成功 被修改的记录条数为 : {}", result);
            } catch (Throwable throwable) {
                log.error("error : {}", throwable);
            } finally {
                jedisUtil.releaseLock(UPDATE_DAY_FIRST_LOGIN);
            }
        } else {
            log.info("dint't get lock");
        }
    }
}
